package ch.ethz.smseth.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;
import ch.ethz.smseth.service.SenderService;

public class FailureActivity extends Activity
{
	public static final String EXTRA_MESSAGE_URI = "messageUri";
	
	private static final int DIALOG_ERROR = 0;
	
	private String _messageText;
	private String _destination;
	private String _errMsg;
	private boolean _sent;
	private Uri _draftUri;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		if (getIntent().getExtras() != null)
		{
			// the intent has been started over the notification manager because something failed
			Bundle extras = getIntent().getExtras();
			_destination = extras.getString(SenderService.EXTRA_DEST);
			_messageText = extras.getString(SenderService.EXTRA_MESSAGE);
			_errMsg = extras.getString(SenderService.EXTRA_ERROR);
			_draftUri = Uri.parse(extras.getString(EXTRA_MESSAGE_URI));
			
			String ns = Context.NOTIFICATION_SERVICE;
			NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
			mNotificationManager.cancel(extras.getInt(SenderService.EXTRA_NOTIFICATION_ID));
			
			showDialog(DIALOG_ERROR);
		}
		else
		{
			_sent = true;
			finish();
		}
	}
	
	@Override
	protected void onPause()
	{
		if (!_sent)
		{	
			Toast toast = Toast.makeText(this, "SMS has been saved to drafts.", Toast.LENGTH_SHORT);
			toast.show();
		}
		
		super.onPause();
	}
	
	protected Dialog onCreateDialog(int id)
	{
	    Dialog dialog;
	    AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    switch(id)
	    {
	    case DIALOG_ERROR:
	    	builder.setMessage(_errMsg)
	    	       .setCancelable(true)
	    	       .setPositiveButton("Retry", new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	               send(SenderService.METHOD_ETH);
	    	           }
	    	       })
	    	       .setNeutralButton("Via Phone", new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	               send(SenderService.METHOD_PHONE);
	    	           }
	    	       })
	    	       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	    	           public void onClick(DialogInterface dialog, int id) {
	    	        	   finish();
	    	           }
	    	       }).setTitle("Error");
	    	dialog = builder.create();

	        break;
	    default:
	        dialog = null;
	    }
	    return dialog;
	}
	
	private void send(int sendMethod)
	{
		Intent sendIntent = new Intent(this, SenderService.class);
		sendIntent.putExtra(SenderService.EXTRA_DEST, new String[]{_destination});
		sendIntent.putExtra(SenderService.EXTRA_MESSAGE, _messageText);
		sendIntent.putExtra(SenderService.EXTRA_METHOD, sendMethod);
		startService(sendIntent);
		
		_sent = true;
		getContentResolver().delete(_draftUri, null, null);
		finish();
	}
}
